package com.gxa.order.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.R;
import com.gxa.order.mapper.RoomTypeMapper;
import com.gxa.order.pojo.hotel.Room;
import com.gxa.order.pojo.hotel.RoomType;
import com.gxa.order.service.RoomTypeService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper,RoomType> implements RoomTypeService{
    @Resource
    private RoomTypeMapper roomTypeMapper;
    @Resource
    private RoomTypeService roomTypeService;
    /**
     * 添加房间类型
     * @param roomType
     * @return
     */
    @Override
    public R addRoomType(RoomType roomType) {
        R r = new R();
        roomType.setCreateTime(LocalDateTime.now());
        int insert = roomTypeMapper.insert(roomType);
        try{
            if(insert>0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 查询房间类型（是否可预约）
     * @param roomTypeId
     * @param hotelId
     * @return
     */
    @Override
    public R queryRoomType(Integer hotelId,Integer roomTypeId) {
        R r = new R();
        try{
            if(hotelId!=null && roomTypeId!=null){   //查看具体房间详情
                RoomType roomType = roomTypeMapper.selectById(roomTypeId);
                r.setCode(200);
                r.setData(roomType);
            }else if(hotelId!=null){  //查询全部房间类型(已上架)
                QueryWrapper<RoomType> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("hotel_id",hotelId);
                List<RoomType> roomTypes = roomTypeMapper.selectList(queryWrapper);
                r.setCode(200);
                r.setData(roomTypes);
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 修改房间类型
     * @param roomType
     * @return
     */
    @Override
    public R updateRoomType(RoomType roomType) {
        R r = new R();
        try {
            int update = roomTypeMapper.updateById(roomType);
            if(update>0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 上下架房型
     * @param roomTypeId
     * @return
     */
    @Override
    public R downRoomType(Integer roomTypeId) {
        R r = new R();
        //查询房型的上下架状态
        RoomType roomType = roomTypeMapper.selectById(roomTypeId);
        Integer roomStatus = roomType.getRoomStatus();
        try{
            UpdateWrapper<RoomType> updateWrapper = new UpdateWrapper<>();
            if(roomStatus==0) {//房型是上架，改为下架状态
                updateWrapper.eq("room_type_id",roomTypeId)
                        .set("room_status",1);
                boolean down = this.update(updateWrapper);
                if(down){
                    r.setCode(200);
                    r.setMsg("success");
                }
            }else {//房型是下架的，改为上架
                updateWrapper.eq("room_type_id",roomTypeId)
                        .set("room_status",0);
                boolean down = this.update(updateWrapper);
                if(down){
                    r.setCode(200);
                    r.setMsg("success");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 修改房型已用数量（添加使用）
     * @return
     */
    @Override
    public void addRoomUsed(Integer roomTypeId) {
        RoomType roomType = roomTypeMapper.selectById(roomTypeId);
        Integer roomUsed = roomType.getRoomUsed();
        roomType.setRoomUsed(roomUsed+1);
        roomTypeMapper.updateById(roomType);
    }

    /**
     * 修改房型已用数量（减少使用）
     * @return
     */
    @Override
    public void downRoomUsed(Integer roomTypeId) {
        RoomType roomType = roomTypeMapper.selectById(roomTypeId);
        Integer roomUsed = roomType.getRoomUsed();
        roomType.setRoomUsed(roomUsed-1);
        roomTypeMapper.updateById(roomType);
    }
}
